package futbol.tacticas.concursantes.laponiafc;

import futbol.General;

/**
 * 
 * @author Galderic Punti
 * 
 * @date 06-06-2008
 * 
 */
public class MovimientoBola extends Movimiento {

	protected int turns = 1;
	protected double angle;
	protected Jugador ultimoContrarioAGolpear;

	public MovimientoBola(double x0, double y0, double x1, double y1) {
		super(x0, y0, x1, y1);
		angle = General.getAngulo(x0, y0, x1, y1);
	}

	public MovimientoBola(double x0, double y0, double x1, double y1, int speed) {
		super(x0, y0, x1, y1, speed);
		angle = General.getAngulo(x0, y0, x1, y1);
	}

	public final void reset() {
		super.reset();
		turns = 1;
		angle = 0;
		ultimoContrarioAGolpear = null;
	}

	@Override
	public final void addPoint(double x, double y, Jugador[] players) {

		double dx = x - cur.x;
		double dy = y - cur.y;

		double nangle = General.getAngulo(cur.x, cur.y, x, y);

		if (Math.abs(nangle - angle) < .1f) {
			if ((x != cur.x) || (y != cur.y)) {
				cur.setLocation(x, y);
				this.angle = General.getAngulo(origin.x, origin.y, x, y);
				turns++;
			}
		} else {
			origin.setLocation(cur.x, cur.y);
			cur.setLocation(x, y);
			this.angle = nangle;
			turns = 1;

			this.ultimoContrarioAGolpear = help.jugadorMasCercanoAPunto(
					players, x, y, Equipo.CONTRARIO, false);

		}

		// update speed
		dx = cur.x - origin.x;
		dy = cur.y - origin.y;

		double d = Math.round(Math.sqrt(dx * dx + dy * dy));

		if (turns==0) {
			turns = 1;
		}
		
		// desarrollo de turns*v0 - sum(i,0,turns)=d
		double v0 = (2 * d + (turns - 1) * turns) / (2 * turns);

		setSpeed((int) Math.round(v0) - (turns - 1));
	}

	public double getAngle() {

		return angle;
	}

}
